/**
 * Promise/A+ 规范：
 * 1.promise 有三个状态：pending，fulfilled，or rejected；「规范 Promise/A+ 2.1」
 * 2.new promise时， 需要传递一个executor()执行器，执行器立即执行；
 * 3.executor接受两个参数，分别是resolve和reject；
 * 4.promise 的默认状态是 pending；
 * 5.promise 有一个value保存成功状态的值，可以是undefined/thenable/promise；「规范 Promise/A+ 1.3」
 * 6.promise 有一个reason保存失败状态的值；「规范 Promise/A+ 1.5」
 * 7.promise 只能从pending到rejected, 或者从pending到fulfilled，状态一旦确认，就不会再改变；
 * 8.promise 必须有一个then方法，then 接收两个参数，分别是 promise 成功的回调onFulfilled, 和 promise 失败的回调 onRejected；「规范 Promise/A+ 2.2」
 * 9.如果调用 then 时，promise 已经成功，则执行onFulfilled，参数是promise的value；
 * 10.如果调用 then 时，promise 已经失败，那么执行onRejected, 参数是promise的reason；
 * 11.如果 then 中抛出了异常，那么就会把这个异常作为参数，传递给下一个 then 的失败的回调onRejected；
 */

const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
class myPromise {
    constructor(executor) {
        this.value = undefined
        this.reason = undefined
        this.status = PENDING
        this.onFulfilledhandles=[]
        this.onRejectedhandles=[]
        let resolve = (value) => {
            if (this.status === PENDING) {
                this.status = FULFILLED
                this.value = value
                this.onFulfilledhandles.forEach(fn=>fn())
            }
        }
        let reject = (reason) => {
            if (this.status === PENDING) {
                this.status = REJECTED
                this.reason = reason
                this.onRejectedhandles.forEach(fn=>fn())
            }

        }
        try {
            executor(resolve, reject)
        } catch (error) {
            reject(error)
        }
        
    }
    then(onFulfilled, onRejected) {
        if (this.status === FULFILLED) {
            onFulfilled(this.value)

        }
        if (this.status === REJECTED) {
            onRejected(this.reason)
        }
        if(this.status ===PENDING){
            this.onFulfilledhandles.push(()=>{
                onFulfilled(this.value)
            })
            this.onRejectedhandles.push(()=>{
                onRejected(this.reason)
            })
            console.log(this.onFulfilledhandles,this.onRejectedhandles,this.value,this.reason)
        }

    }
}

 
const promise = new myPromise((resolve, reject) => {
    setTimeout(() => {
      resolve('成功');
    },1000);
  }).then(
    (data) => {
      console.log('success', data)
    },
    (err) => {
      console.log('faild', err)
    }
  )

  {
     
    const PADDING = "padding"
    const RESOLVE ='resolve'
    const REJECT = 'reject'
    class MPromise{
        constructor(exec){
            this.status = PADDING
            this.value = undefined
            this.reason= undefined
            this.onresolved = []
            this.onrejected = []
            let resolve = (value)=>{
                if(this.status===PADDING){
                    this.status = RESOLVE
                    this.value = value
                    this.onresolved.forEach(fn=>fn())
                }
            }
            let reject = (reason)=>{
                if(this.status===PADDING){
                    this.status = REJECT
                    this.reason = reason
                }
            }

            exec(resolve,reject)
        }
        then(resolved,rejected){
            if(this.status===PADDING){
                console.log(this.value)
                this.onresolved.push(()=>{resolved(this.value)})
            }
            if(this.status===RESOLVE){
                resolved(this.value)
            }
            if(this.status===REJECT){
                rejected(this.reason)
            }
        }
    }  
    
    function MyNew(){
        let obj = {}
        let arg = [].shift.call(arguments)
        let args = arguments
        obj.__proto__ = arg.prototype
        let res =  arg.apply(obj,args)
        return typeof res ==="object"? res :obj
    }

    function quickSort(arr){
        if(arr.length<=1){
            return arr
        }
        var left=[],rigth=[];
        var middle_len = Math.floor(arr.length/2) 
        var middle = arr.splice(middle_len,1)[0]
        for(let i = 0,len =arr.length;i< len;i++ ){
            if(arr[i]<middle){
                left.push(arr[i])
            }else rigth.push(arr[i])
        }
        return quickSort(left).concat([middle],quickSort(rigth))
    }
  }